package com.noble.glamour.ui.start;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.noble.glamour.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.library.base.BaseActivity;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.logic.baseui.custom.ViewPagerAdapter;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.third.ThirdConstant;
import com.noble.glamour.third.auth.OauthBack;
import com.noble.glamour.ui.utils.NoDoubleClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 注册登陆页面
 */
public class NavUserAct extends BaseActivity {
    private OauthBack oauthBack;

    private LinearLayout dot_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isCanBack(false);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.p1_nav_act);
        initView();
        initCallback();
    }

    private void initCallback() {
        if (oauthBack == null) {
            oauthBack = new OauthBack(this);
            ModuleMgr.getAuthMgr().setCallback(oauthBack);
        }
    }

    private void initView() {
        findViewById(R.id.login_wx).setOnClickListener(clickListener);
        findViewById(R.id.login_phone).setOnClickListener(clickListener);
        dot_layout = (LinearLayout) findViewById(R.id.dot_layout);
        initViewPager();
    }

    private void initViewPager() {
        initDots();
        ViewPager vp_head = (ViewPager) findViewById(R.id.vp_head);
        NavHeadOne headOne = new NavHeadOne(this);
        NavHeadTwo headTwo = new NavHeadTwo(this);

        List<View> list = new ArrayList<>();
        list.add(headOne.getContentView());
        list.add(headTwo.getContentView());
        vp_head.setAdapter(new ViewPagerAdapter(list));
        vp_head.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < dot_layout.getChildCount(); i++) {
                    if (i == position) {
                        dot_layout.getChildAt(i).setSelected(true);
                    } else {
                        dot_layout.getChildAt(i).setSelected(false);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void initDots() {
        for (int j = 0; j < 2; j++) {
            dot_layout.addView(initDot());
        }
        dot_layout.getChildAt(0).setSelected(true);
    }

    private View initDot() {
        return LayoutInflater.from(this).inflate(R.layout.view_pager_dot, null);
    }

    private NoDoubleClickListener clickListener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            initCallback();
            switch (v.getId()) {
                case R.id.login_phone:
                    UIShow.showRegMobileAct(NavUserAct.this);
                    break;

                case R.id.login_wx:
                    LoadingDialog.show(NavUserAct.this, "请稍候");
                    ModuleMgr.getAuthMgr().onLogin(ThirdConstant.WECHAT);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onBackPressed() {
        ModuleMgr.getLoginMgr().clearCookie();
        super.onBackPressed();
    }

    @Override
    protected boolean isImmersionBar() {
        return false;
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (ThirdConstant.mPlatform == ThirdConstant.WECHAT)
            LoadingDialog.closeLoadingDialog();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ModuleMgr.getAuthMgr().release();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // QQ登录回调
        ModuleMgr.getAuthMgr().onActivityResult(requestCode, resultCode, data);
    }
}
